<?php

/**
 * default_recommend actions.
 *
 * @package    jiazhang
 * @subpackage default_recommend
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class default_recommendActions extends sfActions
{

  public function executeIndex(sfWebRequest $request)
  {
      $this->default_recommend = Doctrine::getTable('DefaultRecommend')->findAll();
  }
  
  public function executeAdd(sfWebRequest $request)
  {
      
  }
  
  public function executeDel(sfWebRequest $request)
  {
      $this->forward404Unless($article = Doctrine::getTable('DefaultRecommend')->findOneById($id = $request->getParameter('id',0)));
      $article->delete();
      $this->getUser()->setFlash('success', '删除成功！');
      $this->redirect('default_recommend/index');
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    if($request->isMethod('POST')){
          $title = trim($request->getParameter('title',''));
          $url = trim($request->getParameter('url',''));
          $cover = $request->getFiles('cover');
          $position = (int)$request->getParameter('position','');
          
          $has = Doctrine::getTable('DefaultRecommend')->findOneByPosition($position);
          
          if($has){
                        $this->getUser()->setFlash('error', '这个位置已经有数据了，请选择其他位置！');
                        $this->redirect('default_recommend/add');
          }
          
          $recommend = new DefaultRecommend();
          $is_save = $recommend->setTitle($title)->setUrl($url)->setPosition($position)->trySave();
          
          if($is_save){
              
              
              if($cover['error'] == 0){
              
                    $file_name = $cover['name'];
                    $file_ext_tmp = explode('.', $file_name);
                    $file_ext = $file_ext_tmp[count($file_ext_tmp)-1];
                    $allow_file_type = array('jpg','bmp','png','gif','jpeg');

                    if(in_array($file_ext, $allow_file_type)){
                        $year = date('Y',time());
                        $month = date('m',time());
                        $day = date('d',time());
                        $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.'.$file_ext;
                        $upyun_save_path = '/default_recommend/'.$year.'/'.$month.'/'.$save_file_name;
                        $upyun = new UpYun(sfConfig::get('app_upyun_space'),sfConfig::get('app_upyun_user') , sfConfig::get('app_upyun_pwd'));
                        $image_url=  '';

                        try {
                            $fh = fopen($cover['tmp_name'], 'rb');
                            $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
                            $image_url=  sfConfig::get('app_img_url').$upyun_save_path;
                            fclose($fh);
                        }
                        catch(Exception $e) {
                            echo $e->getCode();
                            echo $e->getMessage();
                        }
                    }else{
                        $this->getUser()->setFlash('error', '图片类型错误！');
                        $this->redirect('default_recommend/add');
                    }
                   $recommend->setCover($image_url)->save();  
              }
             
              $this->getUser()->setFlash('success', '数据保存成功！');
          }
          $this->redirect('default_recommend/index');
    }
  }
}
